package com.hxzy.controller;

import com.hxzy.entity.OrderlyDTO;
import lombok.extern.log4j.Log4j2;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author gityyge
 * @Description 顺序消息（要求消息队列FIFO 先进先出）
 * @Date 2023/8/10 14:10
 * @Version 1.0
 */
@Log4j2
@RestController
public class ProducerOrderlyMessageController {

    private final String OrderLYTAG="orderly:bun";
    private AtomicInteger  atomicInteger=new AtomicInteger(100);

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @GetMapping(value = "/send/orderly")
    public String sendOrderlyMessage(){

        List<OrderlyDTO>  arr=new ArrayList<>();
        arr.add(OrderlyDTO.builder().name("包子").step("1、和面").build());
        arr.add(OrderlyDTO.builder().name("包子").step("2、定型").build());
        arr.add(OrderlyDTO.builder().name("包子").step("3、上炉蒸").build());
        arr.add(OrderlyDTO.builder().name("包子").step("4、完成").build());

        for(OrderlyDTO dto: arr){
            log.info(dto.toString());
            SendResult sendResult = this.rocketMQTemplate.syncSendOrderly(OrderLYTAG, dto, "KEY_" + atomicInteger.getAndIncrement());
            log.info(sendResult.toString());
        }

        return "发送顺序消息完成";
    }

}

